﻿using System;
using System.IO;
using System.Xml.Serialization;
using System.Text;
using System.Globalization;

namespace AppFx.BizTalk.Serialization
{
    /// <summary>
    /// Helper class for obtaining response messages
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class GenericXmlSerializer<T>
    {
        /// <summary>
        /// Generic method to get the response message
        /// </summary>        
        /// <param name="defaultNamespace"></param>
        /// <param name="xml"></param>
        /// <returns></returns>
        public T Deserialize(string xml, string defaultNamespace = null)
        {
            XmlSerializer serializer;
         
            if(string.IsNullOrEmpty(defaultNamespace))
                serializer = new XmlSerializer(typeof(T));                
            else
                serializer = new XmlSerializer(typeof(T), defaultNamespace);                

            using (MemoryStream stream = new MemoryStream())
            {
                StreamWriter writer = new StreamWriter(stream);
                writer.Write(xml);
                writer.Flush();
                stream.Seek(0, SeekOrigin.Begin);
                return (T)serializer.Deserialize(stream);                  
            }
        }
        /// <summary>
        /// Generic method to get the response message
        /// </summary>        
        /// <param name="defaultNamespace"></param>
        /// <param name="xml"></param>
        /// <returns></returns>
        public T Deserialize(Stream stream, string defaultNamespace = null)
        {
            XmlSerializer serializer;

            if (string.IsNullOrEmpty(defaultNamespace))
                serializer = new XmlSerializer(typeof(T));
            else
                serializer = new XmlSerializer(typeof(T), defaultNamespace);

            stream.Seek(0, SeekOrigin.Begin);
            return (T)serializer.Deserialize(stream);            
        }
        /// <summary>
        /// Generic method to serialize an object
        /// </summary>
        /// <param name="toSerialize"></param>
        /// <returns></returns>
        public string SerializeToString(object toSerialize)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));

            using (MemoryStream stream = new MemoryStream())
            {
                serializer.Serialize(stream, toSerialize);
                stream.Seek(0, SeekOrigin.Begin);
                StreamReader rdr = new StreamReader(stream);
                return rdr.ReadToEnd();
            }
        }
        /// <summary>
        /// Generic method to serialize an object
        /// </summary>
        /// <param name="toSerialize"></param>
        /// <returns></returns>
        public Stream SerializeToStream(object toSerialize)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            var stream = new MemoryStream();
            serializer.Serialize(stream, toSerialize);
            stream.Seek(0, SeekOrigin.Begin);
            return stream; 
        }
    }
}



